<!-- 充值 -->
<template>
	<view>
		<u-navbar :isFixed="true" title="充值" :borderBottom="false"></u-navbar>
		<view class="block"></view>
		<view class="wrap">
			<view class="tips">请完善以下充值信息</view>
			<view class="charge flex_bet">
				<view class="name">
					<view>充值金额</view>
					<view class="balance">余额￥{{wallet}}</view>
				</view>
				<input type="digit" v-model="withdrawal_money" placeholder="请输入充值金额">
			</view>
			<view class="block"></view>
			<view class="title">选择充值方式</view>
			<view class="flex_bet_align type" @click="chooseType(1)">
				<view>
					<u-icon name="weixin-circle-fill" color="#00C900" size="40"></u-icon>
					<text>微信</text>
				</view>
				<view class="iconfont" :class="type==1?'icon-xuanzhong':'icon-xuanzhongyuandian'"></view>
			</view>
			<view class="flex_bet_align type" @click="chooseType(2)">
				<view>
					<u-icon name="zhifubao-circle-fill" color="#06B4FD" size="36"></u-icon>
					<text>支付宝</text>
				</view>
				<view class="iconfont" :class="type==2?'icon-xuanzhong':'icon-xuanzhongyuandian'"></view>
			</view>
		</view>
		<view style="height:140rpx;"></view>
		<view class="btn" @click="submit">提交</view>
	</view>
</template>

<script>
	var _this;
	export default {
		data() {
			return {
				wallet: "",
				withdrawal_money: "",
				type: 1,
			}
		},
		onLoad(options) {
			_this = this
			_this.getdata()
		},
		methods: {
			// 获取数据
			getdata() {
				_this.$api.get("balance", {}).then(data => {
					_this.wallet = data
				});
			},
			// 提交
			submit() {
				let info = {}
				if (_this.type == 1) {
					info = {
						money: _this.withdrawal_money,
						pay_tuneup_type: 2,
						pay_type: _this.type
					}
				} else {
					info = {
						money: _this.withdrawal_money,
						pay_type: _this.type
					}
				}

				_this.click_shake(function() {
					let data = [{
						value: _this.withdrawal_money,
						type: 1,
						title: "请填写充值金额"
					}]
					if (_this.$plug.verify(data)) {
						_this.$api.post("walletrecharge", info).then(data => {
							if (_this.type == 1) {
								console.log(data)
								uni.requestPayment({
									"provider": "wxpay",
									"orderInfo": {
										"appid": data.appid,
										"noncestr": data.noncestr,
										"package": data.package,
										"partnerid": data.partnerid,
										"prepayid": data.prepayid,
										"timestamp": data.timestamp,
										"sign": data.sign
									},
									success(res) {
										wx.showModal({
											title: '提示',
											content: '充值成功',
											showCancel: false,
											success: function(res) {
												wx.navigateBack({
													delta: 1
												})
											}
										})
									},
									fail(e) {
										console.log(e);
										wx.showModal({
											title: '提示',
											content: '购买人数过多，等待一下再支付',
											showCancel: false
										})
									}
								})
							} else if (_this.type == 2) {
								console.log(data)
								uni.requestPayment({
									provider: 'alipay',
									orderInfo: data.info,
									success: function(res) {
										console.log('success:' + JSON.stringify(res));
									},
									fail: function(err) {
										console.log('fail:' + JSON.stringify(err));
									}
								});
							}
						});
					}
				})
			},
			// 防抖点击
			click_shake(fn) {
				if (!_this.shake) {
					fn && typeof fn === 'function' && fn()
					_this.shake = true
					setTimeout(function() {
						_this.shake = false
					}, 500)
				}
			},
			chooseType(e) {
				this.type = e
			}
		}
	}
</script>

<style lang="scss" scoped>
	page {
		font-family: PingFang SC;
	}

	.block {
		height: 10rpx;
		background-color: #F2F2F2;
	}

	.wrap {
		padding: 0 30rpx;

		.tips {
			font-size: 24rpx;
			color: #999999;
			line-height: 100rpx;
			border-bottom: 1rpx solid #EEEEEE;
		}

		.charge {
			padding: 30rpx 0 40rpx;

			.name {
				font-size: 30rpx;
				text-align: left;
				color: #000000;

				.balance {
					font-size: 24rpx;
					color: #666666;
					margin-top: 10rpx;
				}
			}

			input {
				width: 208rpx;
				font-size: 30rpx;
				text-align: right;
			}
		}

		.title {
			font-size: 26rpx;
			color: #666666;
			line-height: 100rpx;
		}

		.add {
			font-size: 26rpx;
			font-weight: 500;
			color: #333333;
			line-height: 100rpx;
		}

		.type {
			padding: 32rpx 0;

			text {
				margin-left: 20rpx;
			}

			.iconfont {
				color: #8E8E8E;
				font-size: 34rpx;
			}

			.icon-xuanzhong {
				color: #38BE8C;
			}
		}
	}

	.btn {
		width: 680rpx;
		height: 90rpx;
		background: linear-gradient(86deg, #38be8c 0%, #05adbb 100%);
		border-radius: 45rpx;
		font-size: 34rpx;
		font-weight: 700;
		text-align: center;
		color: #ffffff;
		line-height: 90rpx;
		position: fixed;
		left: 28rpx;
		bottom: 46rpx;
	}
</style>
